import { GameObject, getBehaviourClassByName, getGameObjectById } from "../../engine";
import { System } from "./System";
import { Transform } from "../Transform";
import { RigidBody } from "../../behaviours/RigidBody";
import { b2Contact, b2ContactImpulse, b2ContactListener, b2Manifold, b2ParticleBodyContact, b2ParticleContact, b2ParticleSystem, b2Shape } from "@flyover/box2d";
import { PhysicsSystem } from "../../PhysicsSystem";
import { AnimationBehaviour } from "../../behaviours/AnimationBehaviour";
import { Scene2Binding } from "../../behaviours/Scene2Binding";
import { Scene3Binding } from "../../behaviours/Scene3Binding";
import { Scene4Binding } from "../../behaviours/Scene4Binding";
import { Scene5Binding } from "../../behaviours/Scene5Binding";
import { Scene6Binding } from "../../behaviours/Scene6Binding";
import { Scene7Binding } from "../../behaviours/Scene7Binding";
import { Scene8Binding } from "../../behaviours/Scene8Binding";
import { HeartBinding } from "../../behaviours/HeartBinding";
import { BitmapRenderer } from "../BitmapRenderer";
import { BoxCollider } from "../../behaviours/Collider";
import { Walkable } from "../../behaviours/Walkable";
import { Scene2Binding_end } from "../../behaviours/Scene2Binding_end";
import { Scene2Binding_start } from "../../behaviours/Scene2Binding_start";
import { CollectionBinding } from "../../behaviours/CollectionBinding";
import { Scene1Binding } from "../../behaviours/Scene1Binding";
import { Scene7Binding2 } from "../../behaviours/Scene7Binding2";
import { Scene3Binding_end } from "../../behaviours/Scene3Binding_end";
import { Scene4Binding_end } from "../../behaviours/Scene4Binding_end";
import { Scene5Binding_end } from "../../behaviours/Scene5Binding_end";
import { Scene7Binding_start } from "../../behaviours/Scene7Binding_start";
import { Scene7Binding_mid } from "../../behaviours/Scene7Binding_mid";
import { Scene7Binding_end } from "../../behaviours/Scene7Binding_end";
import { Scene8Binding_end } from "../../behaviours/Scene8Binding_end";
import { boolean } from "../validators/boolean";
import { PortalBinding } from "../../behaviours/PortalBinding";
import { defeatScene2Binding } from "../../behaviours/defeatScene2Binding";
import { defeatScene3Binding } from "../../behaviours/defeatScene3Binding";
import { defeatScene5Binding } from "../../behaviours/defeatScene5Binding";
import { defeatScene7Binding } from "../../behaviours/defeatScene7Binding";
import { DamageScene4Binding } from "../../behaviours/DamageScene4Binding";
import { DamageScene6Binding } from "../../behaviours/DamageScene6Binding";
import { DamageScene7Binding } from "../../behaviours/DamageScene7Binding";
import { DamageScene8Binding } from "../../behaviours/DamageScene8Binding";
import { Sound } from "../../behaviours/sound";
import { defeatScene4Binding } from "../../behaviours/defeatScene4Binding";
import { DamageScene2Binding } from "../../behaviours/DamageScene2Binding";
import { DamageScene3Binding } from "../../behaviours/DamageScene3Bindings";
import { DamageScene5Binding } from "../../behaviours/DamageScene5Binding";
import { GameObjectAcitivable } from "../../behaviours/GameObjectAcitivable";

export class GamePlaySystem extends System implements b2ContactListener{


    public BeginContact(contact: b2Contact<b2Shape, b2Shape>): void {




        const bodyA = contact.GetFixtureA().GetBody().GetUserData();
        const bodyB = contact.GetFixtureB().GetBody().GetUserData();
        const parent = this.rootGameObject
        const index = parent.children.length;//test
        
        const portal2 = getGameObjectById('portal2')
        const portal3 = getGameObjectById('portal3')
        const portal5 = getGameObjectById('portal5')
        const portal7 = getGameObjectById('portal7_1')


        //场景2收集药水，实现碰撞药水消失
        if (bodyA.id==='Alice' && bodyB.id==='potion1' || bodyA.id==='potion1' && bodyB.id==='Alice') {
            const potion1=getGameObjectById('potion1')
            potion1.getBehaviour(CollectionBinding).IsActive=false
            const potion1Rigidbody = potion1.getBehaviour(RigidBody);
            potion1.removeBehaviour(potion1Rigidbody);  
            portal2.getBehaviour(PortalBinding).isSatisfied = true
        }
        //场景3收集药水，实现碰撞药水消失
        if (bodyA.id==='Alice' && bodyB.id==='potion2' || bodyA.id==='potion2' && bodyB.id==='Alice') {
            const potion2=getGameObjectById('potion2') 
            potion2.getBehaviour(CollectionBinding).IsActive=false
            const potion2Rigidbody = potion2.getBehaviour(RigidBody);
            potion2.removeBehaviour(potion2Rigidbody);   
            portal3.getBehaviour(PortalBinding).isSatisfied = true  
        }

        //场景5收集蛋糕，实现碰撞蛋糕消失
        if (bodyA.id==='Alice' && bodyB.id==='cake' || bodyA.id==='cake' && bodyB.id==='Alice') {
            const cake=getGameObjectById('cake')
            cake.getBehaviour(CollectionBinding).IsActive=false
            const cakeRigidbody = cake.getBehaviour(RigidBody);
            cake.removeBehaviour(cakeRigidbody);   
            portal5.getBehaviour(PortalBinding).isSatisfied = true    
        }

         //场景7收集眼睛，实现碰撞眼睛消失
         if (bodyA.id==='Alice' && bodyB.id==='eye' || bodyA.id==='eye' && bodyB.id==='Alice') {
            const eye=getGameObjectById('eye')
            eye.getBehaviour(CollectionBinding).IsActive=false
            const cakeRigidbody = eye.getBehaviour(RigidBody);
            eye.removeBehaviour(cakeRigidbody);   
            portal7.getBehaviour(PortalBinding).isSatisfied = true  
        }

        // if (bodyA.id==='Alice' && bodyB.id==='potion2' || bodyA.id==='potion2' && bodyB.id==='Alice') {
        //     const potion2=getGameObjectById('potion2')
        //     potion2.getBehaviour(CollectionBinding).IsActive=false
        //     const potion2Rigidbody = potion2.getBehaviour(RigidBody);
        //     console.log("potion2Rigdbody:" + potion2Rigidbody)
        //     potion2.removeBehaviour(potion2Rigidbody);     
        // }


        //场景2未收集到药水，弹出收集药水提示
        if (bodyA.id==='Alice' && bodyB.id==='portal2' &&  !portal2.getBehaviour(PortalBinding).isSatisfied|| bodyA.id==='portal2' && bodyB.id==='Alice' &&  !portal2.getBehaviour(PortalBinding).isSatisfied) {
            //跳轉到場景2失敗場景
             const currentScene=getGameObjectById('Scene2')
             const defeatScene2 = new DamageScene2Binding()
             const nextScene = this.gameEngine.createNewPrefab(defeatScene2)
             parent.removeChild(currentScene)
             parent.addChild(nextScene)
        }

         //场景3未收集到药水，弹出收集药水提示
         if (bodyA.id==='Alice' && bodyB.id==='portal3' &&  !portal3.getBehaviour(PortalBinding).isSatisfied|| bodyA.id==='portal3' && bodyB.id==='Alice' &&  !portal3.getBehaviour(PortalBinding).isSatisfied) {
            //跳轉到場景3失敗場景
             const currentScene=getGameObjectById('Scene3')
             const defeatScene3 = new DamageScene3Binding()
             const nextScene = this.gameEngine.createNewPrefab(defeatScene3)
             parent.removeChild(currentScene)
             parent.addChild(nextScene)
        }

        //场景5未收集到蛋糕，弹出收集蛋糕提示
        if (bodyA.id==='Alice' && bodyB.id==='portal5' &&  !portal5.getBehaviour(PortalBinding).isSatisfied|| bodyA.id==='portal5' && bodyB.id==='Alice' &&  !portal5.getBehaviour(PortalBinding).isSatisfied) {
            //跳轉到場景5失敗場景
             const currentScene=getGameObjectById('Scene5')
             const defeatScene5 = new DamageScene5Binding()
             const nextScene = this.gameEngine.createNewPrefab(defeatScene5)
             parent.removeChild(currentScene)
             parent.addChild(nextScene)
        }

         //场景7未收集到眼睛，弹出收集眼睛提示
         if (bodyA.id==='Alice' && bodyB.id==='portal7_1' &&  !portal7.getBehaviour(PortalBinding).isSatisfied|| bodyA.id==='portal7_1' && bodyB.id==='Alice' &&  !portal7.getBehaviour(PortalBinding).isSatisfied) {
            //跳轉到場景7失敗場景
             const currentScene=getGameObjectById('Scene7_1')
             const defeatScene7 = new DamageScene7Binding()
             const nextScene = this.gameEngine.createNewPrefab(defeatScene7)
             parent.removeChild(currentScene)
             parent.addChild(nextScene)
        }

        if (bodyA.id==='Alice' && bodyB.id==='portal1'|| bodyA.id==='portal1' && bodyB.id==='Alice') {
            const currentScene=getGameObjectById('Scene1')
            const scene2Binding_start=new Scene2Binding_start()
            const nextScene = this.gameEngine.createNewPrefab(scene2Binding_start)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)

        }
        if (bodyA.id==='Alice' && bodyB.id==='portal2' &&  portal2.getBehaviour(PortalBinding).isSatisfied|| bodyA.id==='portal2' && bodyB.id==='Alice' &&  portal2.getBehaviour(PortalBinding).isSatisfied) {
            const currentScene=getGameObjectById('Scene2')
            const scene2Binding_end=new Scene2Binding_end()
            const nextScene = this.gameEngine.createNewPrefab(scene2Binding_end)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)
        }
        if (bodyA.id==='Alice' && bodyB.id==='portal3' &&portal3.getBehaviour(PortalBinding).isSatisfied|| bodyA.id==='portal3' && bodyB.id==='Alice' && portal3.getBehaviour(PortalBinding).isSatisfied) {
            const currentScene=getGameObjectById('Scene3')
            const scene3Binding_end=new Scene3Binding_end()
            const nextScene = this.gameEngine.createNewPrefab(scene3Binding_end)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)
        }
        if (bodyA.id==='Alice' && bodyB.id==='portal4' || bodyA.id==='portal4' && bodyB.id==='Alice') {
            const currentScene=getGameObjectById('Scene4')
            const scene4Binding_end=new Scene4Binding_end()
            const nextScene = this.gameEngine.createNewPrefab(scene4Binding_end)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)
        }
        if (bodyA.id==='Alice' && bodyB.id==='portal5' && portal5.getBehaviour(PortalBinding).isSatisfied  || bodyA.id==='portal5' && bodyB.id==='Alice' && portal5.getBehaviour(PortalBinding).isSatisfied ) {
            const currentScene=getGameObjectById('Scene5')
            const scene5Binding_end=new Scene5Binding_end()
            const nextScene = this.gameEngine.createNewPrefab(scene5Binding_end)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)
        }
        if (bodyA.id==='Alice' && bodyB.id==='portal6' || bodyA.id==='portal6' && bodyB.id==='Alice') {
            const currentScene=getGameObjectById('Scene6')
            const scene7Binding_start=new Scene7Binding_start()
            const nextScene = this.gameEngine.createNewPrefab(scene7Binding_start)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)
        }
        if (bodyA.id==='Alice' && bodyB.id==='portal7_1' && portal7.getBehaviour(PortalBinding).isSatisfied || bodyA.id==='portal7_1' && bodyB.id==='Alice' && portal7.getBehaviour(PortalBinding).isSatisfied ) {
            const currentScene=getGameObjectById('Scene7_1')
            const scene7Binding_mid=new Scene7Binding_mid()
            const nextScene = this.gameEngine.createNewPrefab(scene7Binding_mid)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)
        }
        if (bodyA.id==='Alice' && bodyB.id==='portal7_2' || bodyA.id==='portal7_2' && bodyB.id==='Alice') {

            const currentScene=getGameObjectById('Scene7_2')
            const scene7Binding_end=new Scene7Binding_end()
            const nextScene = this.gameEngine.createNewPrefab(scene7Binding_end)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)
        }
        if (bodyA.id==='Alice' && bodyB.id==='portal8' || bodyA.id==='portal8' && bodyB.id==='Alice') {

            const currentScene=getGameObjectById('Scene8')
            const scene8Binding_end=new Scene8Binding_end()
            const nextScene = this.gameEngine.createNewPrefab(scene8Binding_end)
            parent.removeAllChildren();
            // parent.removeChild(currentScene)
            parent.addChild(nextScene)

        }



        //第一关没有带Damage的

        //第二关
        //Alice和bug碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'bug' || bodyA.id == 'bug' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //转到死亡失败场景commDefet
                    const currentScene=getGameObjectById('Scene2')
                    const defeatScene2 = new defeatScene2Binding()
                    const nextScene = this.gameEngine.createNewPrefab(defeatScene2)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene2')
                    const defeatScene2 = new defeatScene2Binding()
                    const nextScene = this.gameEngine.createNewPrefab(defeatScene2)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }


        //第三关
        //Alice和knife、fork碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'knife' || bodyA.id == 'knife' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene3')
                    const defeatScene3 = new defeatScene3Binding()
                    const nextScene = this.gameEngine.createNewPrefab(defeatScene3)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene3')
                    const defeatScene3 = new defeatScene3Binding()
                    const nextScene = this.gameEngine.createNewPrefab(defeatScene3)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }

        if(bodyA.id == 'Alice' && bodyB.id == 'fork' || bodyA.id == 'fork' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene3')
                    const defeatScene3 = new defeatScene3Binding()
                    const nextScene = this.gameEngine.createNewPrefab(defeatScene3)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene3')
                    const defeatScene3 = new defeatScene3Binding()
                    const nextScene = this.gameEngine.createNewPrefab(defeatScene3)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }


        //第四关
        //Alice和mushroom碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'mushroom' || bodyA.id == 'mushroom' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene4')
                    const damageScene4 = new defeatScene4Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene4)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene4')
                    const damageScene4 = new defeatScene4Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene4)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }


        //第五关
        //Alice和specialground碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'Scene5_Ground' || bodyA.id == 'Scene5_Ground' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene5')
                    const defeatScene5 = new defeatScene5Binding()
                    const nextScene = this.gameEngine.createNewPrefab(defeatScene5)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene5')
                    const defeatScene5 = new defeatScene5Binding()
                    const nextScene = this.gameEngine.createNewPrefab(defeatScene5)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }

        //第五关
        //Alice和specialground碰撞
            //Alice是A
            if(bodyA.id == 'Alice' && bodyB.id == 'fork5' || bodyA.id == 'fork5' && bodyB.id == 'Alice') {
                //Alice是A
                if(bodyA.id == 'Alice') {
                    bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                    // bodyB.children[0].getBehaviour(Sound).play();
                    const sound=getBehaviourClassByName('Sound')
                    if(sound.isOpen===true || sound.isOpen===undefined){
                        bodyB.children[0].getBehaviour(Sound).play();
                    }
                    //红心依次减一
                    if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                        getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                    }
                    if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                        getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                    }
                    if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                        getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                        //回到场景起始
                        const currentScene=getGameObjectById('Scene5')
                        const defeatScene5 = new defeatScene5Binding()
                        const nextScene = this.gameEngine.createNewPrefab(defeatScene5)
                        parent.removeChild(currentScene)
                        parent.addChild(nextScene)
                    }
                }
                //Alice是B
                else if(bodyB.id == 'Alice') {
                    bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                    // bodyB.children[0].getBehaviour(Sound).play();
                    const sound=getBehaviourClassByName('Sound')
                    if(sound.isOpen===true|| sound.isOpen===undefined){
                        bodyA.children[0].getBehaviour(Sound).play();
                    }
                    //红心依次减一
                    if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                        getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                    }
                    if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                        getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                    }
                    if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                        getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                        //回到场景起始
                        const currentScene=getGameObjectById('Scene5')
                        const defeatScene5 = new defeatScene5Binding()
                        const nextScene = this.gameEngine.createNewPrefab(defeatScene5)
                        parent.removeChild(currentScene)
                        parent.addChild(nextScene)
                    }
                }
        }


         //第六关
        //Alice和specialGround碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'Scene6_Ground' || bodyA.id == 'Scene6_Ground' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene6')
                    const damageScene6=new DamageScene6Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene6)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene6')
                    const damageScene6=new DamageScene6Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene6)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }
        if(bodyA.id == 'Alice' && bodyB.id == 'axe' || bodyA.id == 'axe' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene6')
                    const damageScene6=new DamageScene6Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene6)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene6')
                    const damageScene6=new DamageScene6Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene6)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }



        //第七关-1
        //Alice和地刺碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'poke' || bodyA.id == 'poke' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }

                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene7_1')
                    const damageScene7=new defeatScene7Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene7)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }

                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene7_1')
                    const damageScene7=new defeatScene7Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene7)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }
        //Alice和火把碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'torch' || bodyA.id == 'torch' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene7_1')
                    const damageScene7=new defeatScene7Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene7)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }

                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene7_1')
                    const damageScene7=new defeatScene7Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene7)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }


        //第八关
        //Alice和specialground碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'Scene8_Ground' || bodyA.id == 'Scene8_Ground' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene8')
                    const damageScene8=new DamageScene8Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene8)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene8')
                    const damageScene8=new DamageScene8Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene8)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }
        //Alice和箭碰撞
        if(bodyA.id == 'Alice' && bodyB.id == 'arrow' || bodyA.id == 'arrow' && bodyB.id == 'Alice') {
            //Alice是A
            if(bodyA.id == 'Alice') {
                bodyA.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyB.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true || sound.isOpen===undefined){
                    bodyB.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyA.getBehaviour(AnimationBehaviour).LifePoint === 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene8')
                    const damageScene8=new DamageScene8Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene8)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
            //Alice是B
            else if(bodyB.id == 'Alice') {
                bodyB.getBehaviour(AnimationBehaviour).LifePoint -=1
                // bodyA.children[0].getBehaviour(Sound).play();
                const sound=getBehaviourClassByName('Sound')
                if(sound.isOpen===true|| sound.isOpen===undefined){
                    bodyA.children[0].getBehaviour(Sound).play();
                }
                //红心依次减一
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===2){
                    getGameObjectById('heart1').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint===1){
                    getGameObjectById('heart2').getBehaviour(HeartBinding).IsActive=false
                }
                if(bodyB.getBehaviour(AnimationBehaviour).LifePoint <= 0) {
                    getGameObjectById('heart3').getBehaviour(HeartBinding).IsActive=false
                    //回到场景起始
                    const currentScene=getGameObjectById('Scene8')
                    const damageScene8=new DamageScene8Binding()
                    const nextScene = this.gameEngine.createNewPrefab(damageScene8)
                    parent.removeChild(currentScene)
                    parent.addChild(nextScene)
                }
            }
        }


        //Alice跳跃后下落碰撞到地面恢复到rightleisure动画状态
        if(bodyA.id == 'Alice' && bodyB.id == 'Scene1_Ground' || bodyA.id == 'Scene1_Ground' && bodyB.id == 'Alice'
            ||bodyA.id == 'Alice' && bodyB.id == 'Scene2_Ground' || bodyA.id == 'Scene2_Ground' && bodyB.id == 'Alice'
            ||bodyA.id == 'Alice' && bodyB.id == 'Scene3_Ground' || bodyA.id == 'Scene3_Ground' && bodyB.id == 'Alice'
            ||bodyA.id == 'Alice' && bodyB.id == 'Scene4_Ground' || bodyA.id == 'Scene4_Ground' && bodyB.id == 'Alice'
            ||bodyA.id == 'Alice' && bodyB.id == 'hat' || bodyA.id == 'hat' && bodyB.id == 'Alice'
            ||bodyA.id == 'Alice' && bodyB.id == 'skull' || bodyA.id == 'skull' && bodyB.id == 'Alice'
            ||bodyA.id == 'Alice' && bodyB.id == 'Scene7_Ground' || bodyA.id == 'Scene7_Ground' && bodyB.id == 'Alice'
            ||bodyA.id == 'Alice' && bodyB.id == 'pillar' || bodyA.id == 'pillar' && bodyB.id == 'Alice') {
            if(bodyA.id == 'Alice') {
                const animationBehaviour=bodyA.getBehaviour(AnimationBehaviour);
                animationBehaviour.action='rightleisure'
            }
            else if(bodyB.id == 'Alice') {
                const animationBehaviour=bodyB.getBehaviour(AnimationBehaviour);
                animationBehaviour.action='rightleisure'
            }
        }


    }
    public EndContact(contact: b2Contact<b2Shape, b2Shape>): void {
    }
    public PreSolve(contact: b2Contact<b2Shape, b2Shape>, oldManifold: b2Manifold): void {
    }
    public PostSolve(contact: b2Contact<b2Shape, b2Shape>, impulse: b2ContactImpulse): void {
    }




    public BeginContactFixtureParticle(system: b2ParticleSystem, contact: b2ParticleBodyContact): void {
    }
    public EndContactFixtureParticle(system: b2ParticleSystem, contact: b2ParticleBodyContact): void {
    }
    public BeginContactParticleParticle(system: b2ParticleSystem, contact: b2ParticleContact): void {
    }
    public EndContactParticleParticle(system: b2ParticleSystem, contact: b2ParticleContact): void {
    }

    onStart(): void {
        console.log('开始游戏')

        this.gameEngine.getSystem(PhysicsSystem).SetContactListener(this)

        

    }

    onUpdate(): void {
        // const cameraGameObject = getGameObjectById('camera');
        // const transform = cameraGameObject.getBehaviour(Transform)
        // transform.x += 1;
    }
}